package com.yc.jpyy.batch.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.batch.dao.BatchDao;
import com.yc.jpyy.batch.service.BatchService;






@Service
public class BatchServiceImpl extends BaseService implements BatchService {
    
	@Autowired
	private BatchDao batchDao;

	

	@Override
	public int addBatchService(Map<String, Object> paramsMap) throws DataAccessException {
		int ret = batchDao.addBatchDao(paramsMap);
		System.out.println(ret);
		return ret;
	}

	@Override
	public int updateBatchService(Map<String, Object> paramsMap) throws DataAccessException {
		int ret = batchDao.updateBatchDao(paramsMap);
		return ret;
		
	}

	@Override
	public Map<String, Object> findBatch(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		List<Map<String,Object>> list = batchDao.findBatch(paramsMap);
		int counts = batchDao.findBatchCount(paramsMap);
		if(list != null && list.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData",list);
			resultMap.put("TotalCount",counts);
			resultMap.put("TotalPage",counts % pagesize >0 ? counts/pagesize + 1 : counts/pagesize);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}

	@Override
	public List<Map<String, Object>> findBatchSelectService(Map<String, Object> paramsMap) throws DataAccessException {
		 return batchDao.findBatchSelectDao(paramsMap);
	}

	@Override
	public RestResponse delBatchService(Map<String, Object> paramsMap,RestResponse response) throws DataAccessException {
		//查询是否有关联子项，否则不能删除
		int counts = batchDao.selectRelation(paramsMap);
		if(counts > 0){
			response.setReturnCode(1);
			response.setListData(-100);
			response.setReturnMsg("有相关子项，不能删除");
		}else{	
			int count = batchDao.delBatchDao(paramsMap);
			if(count <= 0){
				response.setReturnCode(-1);
				response.setReturnMsg("删除失败");
			}else{
				response.setReturnCode(1);
				response.setReturnMsg("删除成功");
			}
		}
		return response;
	}

	@Override
	public int addExamBatchService(Map<String, Object> paramsMap) throws DataAccessException {
		String stuIds = paramsMap.get("stuids").toString();
		System.out.println("stuIds---"+stuIds);
		String[] stuIdsArray = stuIds.split(",");
		int arrayLength = stuIdsArray.length;
		int sum = 0;
		int retValue = 1;
		int ret = 0;
		for (int i = 0; i < stuIdsArray.length; i++) {
			paramsMap.put("stuid", Integer.parseInt(stuIdsArray[i]));
			ret = batchDao.addExamBatchDao(paramsMap);
			sum = sum + ret;
		}
		if (sum != arrayLength) {
			retValue = 0;// 数据异常将返回值设置为0
			throw new SqlSessionException("数据库新增数据与数组接收数据不一致");
		}
		paramsMap.put("stuids", paramsMap.get("stuids").toString().split(","));
		int count = batchDao.updateStudentBase(paramsMap);
		if(count > 0){
			retValue =  count;
		}else{
			return 0;
		}
		return retValue;
	}

	@Override
	public Map<String, Object> findBatchByIdService(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return batchDao.findBatchByIdDao(paramsMap);
	}

	@Override
	public Map<String, Object> getBatchDisatributeList(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		List<Map<String,Object>> list = batchDao.getBatchDisatributeList(paramsMap);
		int counts = batchDao.selectBatchDisatributeCount(paramsMap);
		if(list != null && list.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData",list);
			resultMap.put("TotalCount",counts);
			resultMap.put("TotalPage",counts % pagesize >0 ? counts/pagesize + 1 : counts/pagesize);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}



	
	
	

	



	
	


	
}
